package com.frank.demo.controller;/*
 *@author:xec
 *@date:2022/2/28  8:58
 */

import com.frank.demo.domain.Category;
import com.frank.demo.req.CategoryQueryReq;
import com.frank.demo.req.CategorySaveReq;
import com.frank.demo.resp.CommonResp;
import com.frank.demo.resp.CategoryQueryResp;
import com.frank.demo.resp.PageResp;
import com.frank.demo.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 *  对category表的操作都在此接口下执行
 */

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    CategoryService categoryService;

    /**
     * 查询所有分类的方法
     * @return
     */
    @GetMapping("/getAllCategory")
    public CommonResp selectAllCategory(){
        CommonResp<List<CategoryQueryResp>> resp = new CommonResp<>();
        List<CategoryQueryResp> list = categoryService.getAll();
        resp.setContent(list);
        return resp;
    }

    /**
     * 带条件查询分类
     * @param categoryQueryReq
     * @return
     * 注解 @Valid 开启规则检验
     */
    @GetMapping("/getCategory")
    public CommonResp<PageResp<CategoryQueryResp>> selectEBook(@Valid CategoryQueryReq categoryQueryReq){
        PageResp<CategoryQueryResp> list = categoryService.getCategory(categoryQueryReq);
        CommonResp<PageResp<CategoryQueryResp>> resp = new CommonResp<>();
        resp.setMessage("查询成功");
        resp.setContent(list);
        return resp;
    }

    /**
     * 保存方法
     * @param req
     * @return
     */
    @PostMapping("/save")
    public CommonResp save(@RequestBody CategorySaveReq req) {
        CommonResp resp = new CommonResp<>();
        categoryService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        categoryService.delete(id);
        return resp;
    }
}
